﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var cmdString = "select*from Articles";
            var dt = DbHelper.GetDataTable(cmdString); 
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
         
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var title = this.textBox1.Text;
            var selectString = string.Format("select*from Articles where Title like '%{0}%'",title);
            var dt = DbHelper.GetDataTable(selectString);
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editForm = new EditForm();
            var result = editForm.ShowDialog();
            if (result.Equals(DialogResult.Yes))
            {
                var title = this.textBox1.Text;
                var insertString = string.Format("select*from Articles where Title like '%{0}%'", title);
                var dt = DbHelper.GetDataTable(insertString);
                dataGridView1.DataSource = dt;
                
            }
           
            else 
            {
                MessageBox.Show("添加失败");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {


            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var display = MessageBox.Show("确定删除","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
                if (display.Equals(DialogResult.Yes))
                {
                    var deleteString = string.Format("delete Articles where Id='{0}'", id);
                    var dt = DbHelper.AddOrUpdateOrDelete(deleteString);
                    if (display > 0)
                    { var title = this.textBox1.Text;
                        var sql = string.Format("select*from Articles where Title like '%{0}%'",title);
                        var res = DbHelper.GetDataTable(sql);
                        dataGridView1.DataSource = res;
                        MessageBox.Show("删除成功");
                    }
                    

                }
             

            }  
            else
                {
                    MessageBox.Show("请选择行数进行删除");
                }

        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var time = dataGridView1.SelectedRows[0].Cells["CreateTime"].Value.ToString();
                EditForm editForm = new EditForm(id, title, author, time);
                var res = editForm.ShowDialog();
                if (res.Equals(DialogResult.Yes))
                {
                    var titleTemp = this.textBox1.Text;
                    var sql = string.Format("select*from Articles where Title like '%{0}%'", titleTemp);
                    var dt = DbHelper.GetDataTable(sql);
                    dataGridView1.DataSource = dt;

                }
                else
                {
                    MessageBox.Show("取消更新");
                }

            }
            else 
            {
                MessageBox.Show("请选择行数进行更新");
            }
        }
    }
}
